package com.example.administrator.fourwork;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private UseFragment useFragment;
    private IndexFragment indexFragment;
    private FragmentManager fragementManager;
    private View useLayout;
    private View indexLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        fragementManager=getFragmentManager();
        setTabSelection(0);
    }
    private void initViews() {
        useLayout=findViewById(R.id.bottom_application);
        indexLayout=findViewById(R.id.bottom_report);
        useLayout.setOnClickListener(this);
        indexLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bottom_application:
                setTabSelection(0);
                break;
            case R.id.bottom_report:
                setTabSelection(1);
                break;
            default:
                break;
        }
    }
    private void setTabSelection(int i) {
        clearSelection();
        FragmentTransaction transaction=fragementManager.beginTransaction();
        hideFragments(transaction);
        switch (i){
            case 0:
                useLayout.setBackgroundColor(0xff888888);
                if(useFragment==null){
                    useFragment=new UseFragment();
                    transaction.add(R.id.content,useFragment);
                }else {
                    transaction.show(useFragment);
                }
                break;
            case 1:
                indexLayout.setBackgroundColor(0xff888888);
                if(indexFragment==null){
                    indexFragment=new IndexFragment();
                    transaction.add(R.id.content,indexFragment);
                }else {
                    transaction.show(indexFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if(useFragment!=null){
            transaction.hide(useFragment);
        }
        if(indexFragment!=null){
            transaction.hide(indexFragment);
        }
    }

    private void clearSelection() {
        useLayout.setBackgroundColor(0xffffffff);
        indexLayout.setBackgroundColor(0xffffffff);
    }
}

